/*
 * Created on 2005-11-5
 *
 */
package com.yl.cap.analyst.report.helper.operator;

import jaoso.framework.dao.RoleDAO;

import java.io.FileInputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.context.ApplicationContext;

import com.yl.cap.analyst.config.ContextBean;


public abstract class Operator
{
    public Set getOperator(String fileName) throws Exception
    {
        String content = this.file2String(fileName);
        List strGroups = this.getOperatorFromStr(content);
        return this.getGroup(strGroups);
    }
    
    public List getOperatorName(String fileName) throws Exception
    {
        String content = this.file2String(fileName);
        return this.getOperatorFromStr(content);
    }
    
    public boolean isAccess(String fileName,String groupName)
    {
        List groups;
        try
        {
            groups = this.getOperatorName(fileName);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return groups.contains(groupName);
    }
    
    protected abstract List getOperatorFromStr(String content);

    private String file2String(String fileName) throws Exception
    {
        byte[] byteFile = new byte[1000];
        FileInputStream fis = new FileInputStream(fileName);
        fis.read(byteFile);
        fis.close();
        return new String(byteFile);
    }
    
    private Set getGroup(List groups)
    {
        if (groups == null) return null;
        ApplicationContext ac = ContextBean.springContext;
        RoleDAO dao = (RoleDAO) ac.getBean("groupDAO");
        String group = null;
        Set retSet = new HashSet();
        for (int i = 0; i < groups.size(); i++)
        {
            group = (String)groups.get(i);
            retSet.add(dao.getGroupByDesc(group));
        }
        return retSet;
    }
}
